//
//  TuiTypeViewController.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/9/24.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "TuiTypeViewController.h"
#import "TuiStyleTableViewCell.h"
@interface TuiTypeViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation TuiTypeViewController
{
    float _money;
    int _page;
    RWScrollView * _table;
    UIButton * _button2,* _button3;
    NSMutableArray * _arr;
    UIView *_viewNo;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self jianCeLogin];
}
-(void)headRefresh{
    _page = 1;
    [self requestData:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestData:0];
}
-(void)requestData:(int)first{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
    [requestDic setObject:@"10" forKey:@"pageSize"];
    [requestDic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    [requestDic setObject:[self defaultLoadUserName] forKey:@"userName"];
    [NetWorking NEWjieKouQingQiu:TK_queryrefund ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        [_table headerEndRefreshing];
        [_table footerEndRefreshing];
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                if (first == 1) {
                    _arr =  [[NSMutableArray alloc]init];
                }
                [_arr addObjectsFromArray: dic[Response]];
                [_table reloadData];
                if (_arr.count == 0) {
                    _viewNo.hidden = NO;
                }else{
                    _viewNo.hidden = YES;
                }
            }
        }
    }];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self drawNavWithTitle:@"退款售后" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];
    self.view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.92 alpha:1.00];
    _table =  [[RWScrollView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, KScreenHeight-64) style:UITableViewStyleGrouped];
    _table.delegate = self;
    _table.dataSource = self;
    [_table addHeaderWithTarget:self action:@selector(headRefresh)];
    [_table headerBeginRefreshing];
    [_table addFooterWithTarget:self action:@selector(footerRefresh)];
    _table.separatorStyle = UITableViewCellSelectionStyleNone;
    _table.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.92 alpha:1.00];
    [_table registerClass:[TuiStyleTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
    [self noShopView];
}
-(void)noShopView{
    _viewNo = [[[NSBundle mainBundle]loadNibNamed:@"SH_noShop" owner:self options:nil] lastObject];
    _viewNo.hidden = YES;
    _viewNo.frame = CGRectMake(0, 100, KScreenWidth, 127);
    [_table addSubview:_viewNo];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray * a =_arr[section][@"orderGoodsrMapper"];
    return a.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary * dic = _arr[indexPath.section][@"orderGoodsrMapper"][indexPath.row];
    TuiStyleTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:dic[@"goodsImg"]]];
    cell.title.textColor = MaintextColor;
    cell.title.text = dic[@"goodsName"];
    cell.shuXing.text = dic[@"goodsAttr"];
    return cell;
}
#pragma mark 底部视图
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    _money = 0.0;
    NSArray * arr = _arr[section][@"orderGoodsrMapper"];
    [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        _money += ([obj[@"goodsPrice"] floatValue] +  [obj[@"attr_price"] floatValue]);
    }];
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel * money = [[UILabel alloc]initWithFrame:CGRectMake(0, 15, KScreenWidth-10, 20)];
    money.textAlignment = NSTextAlignmentRight;
     money.textColor = MaintextColor;
    money.font = [UIFont systemFontOfSize:14];
   NSString *a = _arr[section][@"refundMoney1"];
    NSString * qian = [NSString stringWithFormat:@"交易金额: ¥%.2f    退款金额: ¥%.2f",_money,[a floatValue]];
    NSMutableAttributedString * abc = [[NSMutableAttributedString alloc]initWithString:qian];
    [abc addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(qian.length-6, 6)];
    money.attributedText = abc;
    [view addSubview:money];
    UIView * xian = [[UIView alloc]initWithFrame:CGRectMake(0, money.bottom+14, KScreenWidth, 1)];
    xian.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.92 alpha:1.00];
    [view addSubview:xian];
//    _button2 = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth/4*2,xian.bottom+12, KScreenWidth/4-10, 26)];
//    [_button2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//    [_button2 setTitle:@"找相似" forState:UIControlStateNormal];
//    _button2.titleLabel.font = [UIFont systemFontOfSize:13];
//    _button2.layer.cornerRadius=3;
//    _button2.layer.borderColor = [UIColor lightGrayColor].CGColor;
//    _button2.layer.borderWidth=1;
//    [_button2 addTarget:self action:@selector(xiangSi:) forControlEvents:UIControlEventTouchUpInside];
//    _button2.tag = section;
//    [view addSubview:_button2];
    
    _button3 = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth/4*3, xian.bottom+12, KScreenWidth/4-10, 26)];
    _button3.titleLabel.font = [UIFont systemFontOfSize:13];
     [_button3 setTitle:@"钱款去向" forState:UIControlStateNormal];
    _button3.layer.cornerRadius=3;
    _button3.layer.borderColor = [UIColor redColor].CGColor;
    [_button3 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    _button3.layer.borderWidth=1;
    _button3.tag = section;
    [_button3 addTarget:self action:@selector(quXiang:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:_button3];
    return view;
}
#pragma mark 头部试图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 57)];
    UIView * xian = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 7)];
    [headerView addSubview:xian];
    xian.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.92 alpha:1.00];
    headerView.backgroundColor = [ UIColor whiteColor];
//    UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(10, 17, KScreenWidth/3, 30)];
//    title.font = [UIFont systemFontOfSize:14];
    UILabel * title2 = [[UILabel alloc]initWithFrame:CGRectMake(KScreenWidth-150, 17, 140, 30)];
    title2.font = [UIFont systemFontOfSize:14];
    title2.textColor = [UIColor redColor];
    title2.textAlignment = NSTextAlignmentRight;
   NSString * a = _arr[section][@"statusBack"];
    switch ([a intValue]) {
        case 0:
            title2.text = @"审核通过";
            break;
        case 5:
            title2.text = @"审核中";
            break;
        case 6:
            title2.text = @"审核失败";
            break;
        case 8:
            title2.text = @"取消审核";
            break;
        case 1:
          
            break;
        case 2:
           
            break;
        default:
            break;
    }
    [headerView addSubview:title2];
   // [headerView addSubview:title];
   // title.text = @"发多少多少分多少";
   // title.text = array[section][@"shangJiaNAme"];
    return headerView;
}
#pragma mark 头部高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 57;
}
#pragma mark 底部高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 100;
}
#pragma mark 行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 97;
}
//#pragma mark 找相似
//-(void)xiangSi:(UIButton *)button{
//
//}
#pragma mark 钱款去向
-(void)quXiang:(UIButton *)button{
    
}
@end
